#pragma once
#include "task.h"
#include "queue.h"
#include "configuration.h"
#include "error.h"

#define NUM_TASK_MAX 100
#define NUM_REPONSE_MAX 10

class task_queue
{
private:
	queue<task> task_to_execute, task_finished;
	configuration conf;
public:
	task_queue(raw_json_message conf_str) :conf(conf_str) {
		if (!conf.is_all_ready())panic();
	}

	~task_queue() {}

	bool add_task(task t) {
		task_to_execute.push(t);
	}

	void execute_task() {
		if (task_to_execute.len() > 0) {
			auto tmp_task = task_to_execute.pop();
			if (conf.is_valid_task(tmp_task)) {
				task_finished.push(tmp_task.do_work(conf.get_driver(tmp_task)));
			}
			else {

			}
		}
	}

	queue<raw_json_message> generate_reponse() {
		auto res = queue<raw_json_message>();
		for (auto i = 0; i < NUM_REPONSE_MAX&&task_finished.len()>0; i++)
			res.push(task_finished.pop().res_to_json());
		return res;
	}
};